/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-15 11:57:11
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-28 14:47:26
 * @FilePath: \cc-game-development-template\assets\game.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-15 11:57:11
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-17 09:04:56
 * @FilePath: \cc-game-development-template\assets\scripts\game.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Animation, _decorator, Button, Color, Component, EventTouch, find, Label, macro, math, Node, NodeEventType, Prefab, Quat, resources, Sprite, SpriteFrame, Tween, tween, UITransform, v2, Vec2, Vec3, sys, view, instantiate } from 'cc';
import { Constant, EventConst } from '../../../common/Common';
import { App } from '../../../App';
import { LoadMg } from '../../../tools/LoadMg';
import UtilTool from '../../../tools/UtilTool';
import AbstractComponent from '../../../base/AbstractComponent';
import BilliardsGameController from '.././controller/BilliardsGameController';
import { GameModel } from '.././model/GameModel';
import wsTool from '../../../net/ws';
import CommConst from '../../../const/CommConst';
import { Ball } from '../Ball';
import { RigidBodyComponent } from 'cc';
import { RigidBody } from 'cc';
import { GameLayer2 as GameLayer, GameLayer2 } from './GameLayer2';
import GameUtils2 from './GameUtils2';
import { GameBaseModel } from '../GameBaseModel';
import { input } from 'cc';
import { Input } from 'cc';
import { GameTrainLogic } from './GameTrainLogic';
import { random } from 'cc';
import { Role } from '../Role';
import { GAME_RES_LIST } from '../GameManage';
const { ccclass, property } = _decorator;

// 游戏主脚本
@ccclass('GameModel2')
export class GameModel2 extends GameBaseModel {

    gameLayerCtrl : GameLayer = null

    protected onLoad(): void {
        super.onLoad();
    }

    protected onEnable(): void {
        super.onEnable();
    }

    public resList = {}

    gameLogic : GameTrainLogic = null
    
    start() {
       super.start();
       this.cueCtrl.showCue(false)

       this.gameLayerCtrl = this.getGameLayerCtrl(GameLayer2)

       App.LoadMg.loadResList(GAME_RES_LIST, (resList) => {
            this.resList = resList
            this.gameStart()
       })

       this.gameLogic = new GameTrainLogic()
       this.setHoleSwitch(true)

    }

    // 游戏正式开始
    gameStart() {
        tween(this.node).delay(0.5).call(()=>{
            this.gameLogic.startGame()
        }).start()
    }

    update(deltaTime: number) {
        super.update(deltaTime);

        let whiteBall = find('gameNode/game_3d/whiteball')
        
        // UtilTool.printLog("whiteball ball position", whiteBall.worldPosition.y, whiteBall.getComponent(RigidBody).isSleeping)
        // UtilTool.printLog("whiteball ball position", whiteBall.worldPosition.toString(), whiteBall.getComponent(RigidBody).isSleeping)
        
    }

    
    // 收到球进洞事件
    ballInRoleEvent(ballNumber : number, ballNode : Node, role : Role) {
        this.gameLogic.ballInRoleEvent(ballNumber, ballNode, role)
    }

    // 收到开始击球事件
    startHitBallEvent(event) {
        this.gameLogic.startHitBallEvent(event)
    }
    
    // 收到白球碰撞球事件
    whiteBallContactEvent(ballNumber : number) {
        this.isOpenBallStatus = false
        if(!this.gameLogic) {
            return // 游戏开始时会有碰球的情况
        }

        this.gameLogic.whiteBallContactEvent(ballNumber)
    }

    // 收到摆球结束事件
    whiteBallPosEndEvent(whiteBallPos) {
        this.gameLogic.whiteBallPosEndEvent(whiteBallPos)
    }

    // 收到击球结束事件
    hitBallEndEvent() {
        this.gameLogic.hitBallEndEvent()
    }

    /**
     * 游戏结束
     */
    public gameOver(data) {
        
    }

    onDestroy() {
        
    }

}

